package client

import (
	"github.com/mars/internal/admin/core/domain"
)

// ProductView 用于前端展示的商品信息
type ProductView struct {
	ID             string      `json:"_id,omitempty"`
	Name           string      `json:"name"`
	Description    string      `json:"description"`
	Detail         string      `json:"detail"`
	Price          int         `json:"price"`
	Image          string      `json:"image"`
	Images         []string    `json:"images"`
	Type           string      `json:"type"`
	Status         string      `json:"status"`
	Sort           int         `json:"sort"`
	Stock          *int        `json:"stock,omitempty"`
	TicketType     string      `json:"ticketType,omitempty"`
	VipDiscount    interface{} `json:"vipDiscount,omitempty"`
	Specifications interface{} `json:"specifications,omitempty"`
	CreatedBy      string      `json:"createdBy"`
	CreateTime     string      `json:"createTime"`
	UpdateTime     string      `json:"updateTime"`
}

// ToProductView 将domain.Product转换为ProductView
func ToProductView(product *domain.Product) *ProductView {
	if product == nil {
		return nil
	}

	view := &ProductView{
		ID:             product.ID,
		Name:           product.Name,
		Description:    product.Description,
		Detail:         product.Detail,
		Price:          product.Price,
		Image:          product.Image,
		Images:         product.Images,
		Type:           string(product.Type),
		Status:         string(product.Status),
		Sort:           product.Sort,
		Stock:          product.Stock,
		TicketType:     string(product.TicketType),
		VipDiscount:    product.VipDiscount,
		Specifications: product.Specifications,
		CreatedBy:      product.CreatedBy,
		CreateTime:     formatTimestamp(product.CreateTime, TimeFormatDateTime),
		UpdateTime:     formatTimestamp(product.UpdateTime, TimeFormatDateTime),
	}

	return view
}

// ToProductViewList 将domain.Product列表转换为ProductView列表
func ToProductViewList(products []domain.Product) []ProductView {
	views := make([]ProductView, len(products))
	for i, product := range products {
		if view := ToProductView(&product); view != nil {
			views[i] = *view
		}
	}
	return views
}
